package web.myServlet;

import entity.Car.bo.CarBo;
import entity.Car.model.Car;
import entity.User.bo.UserBo;
import entity.User.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import web.beans.CarViewBean;
import web.beans.ProfileBean;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by vanches on 15.1.15.
 */
public class UserServlet extends HttpServlet {
    public static final int SC_NOT_FOUND = 404;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String IsLogout = request.getParameter("id");


            HttpSession session = request.getSession();

            String ResultPage = null; //Страница перехода при успешном
            ProfileBean profileBean = (ProfileBean) session.getAttribute("profileBean");
            ServletContext servletContext = getServletContext();
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            UserBo userBo = (UserBo)ctx.getBean("userBo");
            User user=null;
            if (profileBean == null)
                profileBean = new ProfileBean();



            try {
                user = userBo.getUserById(new Integer(IsLogout));
            }
            catch (Exception e){
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }

            ResultPage = "/myAccount/profile.xhtml";
            profileBean.setUser(user);
            session.setAttribute("profileBean", profileBean);


            ServletContext SC = getServletContext();
            RequestDispatcher Disp = SC.getRequestDispatcher(ResultPage);
            Disp.forward(request, response);
        } catch (Throwable theException) {
            theException.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }


}
